package lock;

import java.util.concurrent.TimeUnit;

/**
 * @author fei
 * @date 2023/1/6 15:19
 * @description java 锁案例
 */
public class Lock8Demo {

    public static void main(String[] args) {
        Phone phone = new Phone();
        Phone phone2 = new Phone();
        new Thread(() -> {
            phone.sendEmail();
        }, "a").start();

        try{ TimeUnit.MILLISECONDS.sleep(200);} catch (InterruptedException e) { e.printStackTrace(); }

        new Thread(() -> {
            phone2.sendSMS();
        }, "b").start();
    }

}

/**
 * 资源类
 */
class Phone {

    public static synchronized void sendEmail(){
        try{ TimeUnit.SECONDS.sleep(3);} catch (InterruptedException e) { e.printStackTrace(); }
        System.out.println("-------sendEmail");
    }

    public synchronized void sendSMS(){
        System.out.println("-------sendSMS");
    }

    public void hello(){
        System.out.println("-------hello");
    }

}
